package com.linyu.huawei.simple;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

/**
 * 2024D-身高排序
 *
 * @author 23087
 */
public class HeightSorting {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("请输入小明的身高：");
        int h = sc.nextInt();
        System.out.print("请输入班级其他同学的数量：");
        int num = sc.nextInt();
        ArrayList<Integer> list = new ArrayList<>();
        System.out.println("请依次输入其他小朋友的身高：");
        for (int i = 0; i < num; i++) {
            list.add(sc.nextInt());
        }
        Collections.sort(list, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                int x1 = Math.abs(o1 - h);
                int x2 = Math.abs(o2 - h);
                if (x1 == x2){
                    return o1 - o2;
                }
                return x1 - x2;
            }
        });
        System.out.println(list);
    }
}